import config from "@/config";
import { getStore } from "@/libs/storage";
import { receiveError } from "@/api/common";
import { loading } from "@/libs/request/helpers";

const baseUrl = config.baseUrl;
/**
 * 上传音频
 * @param	{Files}
 * @return {Promise}
 */
export function upload(
  files = "https://hengxiao-resource.oss-cn-hangzhou.aliyuncs.com/web/resources/audio/hello.mp3",
  args,
  url = "/admin/common/audioCompare"
) {
  let token = getStore({
    name: "access_token",
  });
  loading.show();
  return new Promise(function(resolve, reject) {
    uni.uploadFile({
      url: baseUrl + url,
      filePath: files,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        Authorization: "Bearer " + token,
      },
      formData: args,
      success: function(res) {
        loading.hide();
        if (res.statusCode == 200) {
          let data = JSON.parse(res.data);
          resolve(data);
        } else {
          receiveError({
            errorMsg: JSON.stringify(res),
          });
          reject(res.data);
        }
      },
      fail: function(err) {
        loading.hide();
        console.log(err);
        reject(err);
        receiveError({
          errorMsg: JSON.stringify(err),
        });
      },
    });
  });
}
